package work;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;

/**
 * @author Mark
 * @version 1.0
 * @date 2020/5/17 10:44
 * @description 游戏面板类
 */
public class GamePanel extends JPanel implements KeyListener, ActionListener {

    // 蛇的数据结构
    private int length;

    private int[] snakeX = new int[600];

    private int[] snakeY = new int[600];

    private String initDirection;

    // 游戏状态(默认不开始)
    private boolean isStart = false;

    // 游戏失败
    private boolean isFailure = false;

    // 定时器(ms)
    private Timer timer = new Timer(1000, this);

    // 食物坐标
    private int footX;

    private int footY;

    private Random random = new Random();

    // 初始化积分
    private int score;

    GamePanel() {
        // 初始化
        init();
        // 获得焦点事件
        this.setFocusable(true);
        // 键盘监听事件
        this.addKeyListener(this);
        // 刚开始就监听
        timer.start();
    }

    private void init() {
        length = 3;
        snakeX[0] = 100;
        snakeY[0] = 100;
        snakeX[1] = 75;
        snakeY[1] = 100;
        snakeX[2] = 50;
        snakeY[2] = 100;
        initDirection = "RIGHT";

        // 食物随机存放
        footX = 25 + 25 * random.nextInt(34);
        footY = 75 + 25 * random.nextInt(24);
        score = 0;
    }

    /**
     * 键盘监听事件
     *
     * @param e
     */
    @Override
    public void keyPressed(KeyEvent e) {
        int keyCode = e.getKeyCode();
        if (keyCode == KeyEvent.VK_SPACE) {
            if (isFailure) {

                // 重新开始
                isFailure = false;
            } else {
                isStart = !isStart;
            }
            repaint();
        }

        // 小蛇移动
        if (keyCode == KeyEvent.VK_UP) {
            initDirection = "UP";
        } else if (keyCode == KeyEvent.VK_DOWN) {
            initDirection = "DOWN";
        } else if (keyCode == KeyEvent.VK_LEFT) {
            initDirection = "LEFT";
        } else if (keyCode == KeyEvent.VK_RIGHT) {
            initDirection = "RIGHT";
        }

    }

    /**
     * 事件监听
     *
     * @param e
     */
    @Override
    public void actionPerformed(ActionEvent e) {
        // 开始状态
        if (isStart && (!isFailure)) {
            // 移动
            for (int i = length - 1; i > 0; i--) {
                snakeX[i] = snakeX[i - 1];
                snakeY[i] = snakeY[i - 1];
            }

            if (snakeX[0] == footX && snakeY[0] == footY) {
                length++;
                score = score + 10;
                // 食物随机存放
                footX = 25 + 25 * random.nextInt(34);
                footY = 75 + 25 * random.nextInt(24);
            }

            // 走向
            if ("RIGHT".equals(initDirection)) {
                snakeX[0] = snakeX[0] + 25;
                if (snakeX[0] > 850) {
                    snakeX[0] = 25;
                }
            } else if ("LEFT".equals(initDirection)) {
                snakeX[0] = snakeX[0] - 25;
                if (snakeX[0] > 25) {
                    snakeX[0] = 850;
                }

            } else if ("UP".equals(initDirection)) {
                snakeY[0] = snakeY[0] - 25;
                if (snakeY[0] < 75) {
                    snakeY[0] = 650;
                }
            } else if ("DOWN".equals(initDirection)) {
                snakeY[0] = snakeY[0] + 25;
                if (snakeY[0] > 650) {
                    snakeY[0] = 75;
                }
                repaint();
            }

            // 失败判断
            for (int i = 1; i < length; i++) {
                if (snakeX[0] == snakeX[i] && snakeY[0] == snakeY[i]) {
                    isFailure = true;
                    break;
                }
            }
            timer.start();
        }
    }

    @Override
    protected void paintComponent(Graphics g) {
        // 清屏
        super.paintComponent(g);

        // 绘制静态面板
        this.setBackground(Color.WHITE);

        // 头部相片
        DataCenter.HEAD.paintIcon(this, g, 25, 75);

        // 画一个矩形
        g.fillRect(25, 75, 850, 600);

        // 画食物
        DataCenter.FOOT.paintIcon(this, g, footX, footY);

        // 画积分
        g.setColor(Color.white);
        g.setFont(new Font("微软雅黑", Font.BOLD, 18));
        g.drawString("长度" + length, 750, 35);
        g.drawString("积分" + score, 750, 50);

        // 画小蛇(初始化小蛇)
        if ("RIGHT".equals(initDirection)) {
            DataCenter.RIGHT.paintIcon(this, g, snakeX[0], snakeY[0]);
        } else if ("LEFT".equals(initDirection)) {
            DataCenter.LEFT.paintIcon(this, g, snakeX[0], snakeY[0]);
        } else if ("UP".equals(initDirection)) {
            DataCenter.UP.paintIcon(this, g, snakeX[0], snakeY[0]);
        } else if ("DOWN".equals(initDirection)) {
            DataCenter.DOWN.paintIcon(this, g, snakeX[0], snakeY[0]);
        }
        for (int i = 1; i < length; i++) {
            DataCenter.BODY.paintIcon(this, g, snakeX[i], snakeX[i]);
        }

        // 游戏默认状态不开始
        if (!isStart) {
            g.setColor(Color.white);
            g.setFont(new Font("微软雅黑", Font.BOLD, 40));
            g.drawString("game start", 300, 300);
        }

        // 游戏失败
        if (isFailure) {
            g.setColor(Color.red);
            g.setFont(new Font("微软雅黑", Font.BOLD, 40));
            g.drawString("game over", 300, 300);
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

}
